import utils from "@common/utils/utils";

const isBaseHeader = (el) => {
    let formItemList = el.querySelectorAll('.enter_to_next');
    return formItemList && formItemList.length > 0;
}
const getNextFormItem = (el, maxLength, index) => {
    if (index >= maxLength) {
        return undefined;
    }
    let nextItem = el.querySelector(`.enter_to_next.index_${ index }`);
    if (nextItem) {
        return nextItem;
    }
    return getNextFormItem(el, maxLength, index + 1);
}
const getIndex = (formItem) => {
    let classes = (formItem.className || '').split(' ');
    let filter = classes.filter(t => t.indexOf('index_') === 0);
    if (utils.isNotEmptyList(filter)) {
        return Number(filter[0].replace('index_', ''));
    }
    return -1;
}
const enterToNext4BaseHeader = (el, binding, vNode) => {
    let formItemList = el.querySelectorAll('.enter_to_next');
    for (let i = 0; i < formItemList.length; i++) {
        const formItem = formItemList[i];
        let index = getIndex(formItem);
        if (index < 0) {
            continue;
        }
        let input = formItem.querySelector('input,textarea');
        let currRef = vNode.context.$refs[`search_item_${index}`][0];
        input.addEventListener('keydown', ev => {
            if (ev.keyCode === 13) {
                let nextFormItem = getNextFormItem(el, formItemList.length, index + 1);
                if (nextFormItem) {
                    $(input).parents('.el-input').removeClass('is-focus');
                    let popper = currRef.$refs && currRef.$refs.popper;
                    if (popper) {
                        $(popper.$el).hide();
                    } else {
                        popper = document.body.querySelector(`.date_popper_${index}`);
                        if (popper) {
                            $(popper).hide();
                        }
                    }
                    let nextInput = nextFormItem.querySelector('input,textarea');
                    if (nextInput) {
                        nextInput.removeAttribute('readonly');
                    }
                    let nextIndex = getIndex(nextFormItem);
                    if (nextIndex !== -1) {
                        let nextRef = vNode.context.$refs[`search_item_${nextIndex}`][0];
                        if (nextRef) {
                            setTimeout(() => {
                                nextRef.focus();
                                if ($(nextRef.$el).hasClass('el-range-editor')) {
                                    $(nextRef.$el).find('input').eq(0).focus();
                                }
                            }, 10);
                        }
                    }
                }
            }
        });
    }
};
const enterToNext = (el, binding, vNode) => {
    let list = el.querySelectorAll('input');
    let textarea = el.querySelectorAll('textarea');
    if (list && textarea) {
        list = [ ...list, ...textarea ];
    }
    let popper = vNode.context.$el.querySelectorAll('.el-popper');
    let inputs = [];
    for (let i = 0; i < list.length; i++) {
        const input = list[i];
        let type = $(input).attr('type');
        if ($(input).hasClass('el-select__input')) {
            continue;
        }
        if (!$(input).is(':disabled') && 'hidden' !== type && 'radio' !== type) {
            inputs.push(input);
        }
    }
    //绑定回写事件
    for (let i = 0; i < inputs.length; i++) {
        const input = list[i];
        $(input).attr("focus-index", i);
        if ($(input).parents('.base_header_search_input')) {
            continue
        }
        $(input).off('keydown').on('keydown', ev => {
            if (ev.keyCode === 13) {
                $(ev.target).blur();
                let targetTo = $(ev.target).attr('focus-to');
                if (targetTo) {
                    $(vNode.context.$refs[targetTo].$el).find('input').focus();
                } else {
                    let attrIndex = parseInt($(ev.target).attr('focus-index'));
                    if (attrIndex < inputs.length - 1) {
                        $(ev.target).parents('.el-input').removeClass('is-focus');
                        let isReverse = $(ev.target).parents('.el-input').find('.is-reverse');
                        if (isReverse && isReverse.length > 0) {
                            $(isReverse).removeClass('is-reverse');
                            $(ev.target).attr('readonly', 'readonly');
                            let placeholder = $(ev.target).attr('placeholder');
                            if (placeholder.indexOf('请') !== 0) {
                                $(ev.target).val($(ev.target).attr('placeholder'));
                            }
                        }
                        if (popper && popper.length > 0) {
                            $(popper).hide();
                        }
                        setTimeout(function() {
                            inputs[attrIndex + 1] && inputs[attrIndex + 1].focus();
                        }, 10);
                    }
                }
            }
        });
    }
};
/**
 * 表单回车跳转下一输入框指令
 */
export default {
    inserted(el, binding, vNode) {
        if (isBaseHeader(el)) {
            enterToNext4BaseHeader(el, binding, vNode);
            return;
        }
        enterToNext(el, binding, vNode);
    },
    componentUpdated(el, binding, vNode) {
        if (isBaseHeader(el)) {
            return;
        }
        vNode.context.$nextTick(() => {
            enterToNext(el, binding, vNode);
        });
    },
};
